﻿using System;
using System.Collections.Generic;
using Cos.Core.Model.Membership;
using Cos.Core.Service;
using Cos.Module.Membership.Data;
using Cos.Module.Membership.Exception;
using Microsoft.Practices.Unity;

namespace Cos.Module.Membership
{
    public class MembershipService : ServiceBase, IMembershipService
    {
        /**
         * Membership data access object
         */

        private readonly IMembershipRepository _membershipRepository;

        [InjectionConstructor]
        public MembershipService(IMembershipRepository membershipRepository)
        {
            this._membershipRepository = membershipRepository;
        }

        /**
     * {@inheritDoc}
     */
        //@Override
        public MemberUser GetAuthenticationByUsername(String username, String applicationName)
        {
            // Check for invalid parameters
            if (username == null)
            {
                throw new UserNotFoundException("The username should not be null");
            }
            return _membershipRepository.GetAuthenticationByUsername(username, applicationName);
        }

        /**
         * {@inheritDoc}
         */
        //@Override
        public MemberUser GetAuthenticationByEmail(String email, String applicationName)
        {
            // Check for invalid parameters
            if (email == null)
            {
                throw new UserNotFoundException("The email should not be null");
            }
            return _membershipRepository.GetAuthenticationByEmail(email, applicationName);
        }

        /**
         * {@inheritDoc}
         */
        //@Override
        public MemberUser GetAuthenticationByPkid(String userId, String applicationName)
        {
            // Check for invalid parameters
            if (userId == null)
            {
                throw new UserNotFoundException("The userId should not be null");
            }
            return _membershipRepository.GetAuthenticationByPkid(userId, applicationName);
        }

        /**
         * {@inheritDoc}
         */
        //@Override
        public void OnSuccessLogin(String userId)
        {
            // Check for invalid parameters
            if (userId == null)
            {
                throw new UserNotFoundException("The userId should not be null");
            }
            _membershipRepository.OnSuccessLogin(userId);
        }

        /**
         * {@inheritDoc}
         */
        //@Override
        public void OnFailedlLogin(String userId)
        {
            // Check for invalid parameters
            if (userId == null)
            {
                throw new UserNotFoundException("The userId should not be null");
            }
            _membershipRepository.OnFailedlLogin(userId);
        }

        /**
         * {@inheritDoc}
         */
        // @Override
        public void OnSuccessLogout(String userId)
        {
            // Check for invalid parameters
            if (userId == null)
            {
                throw new UserNotFoundException("The userId should not be null");
            }
            _membershipRepository.OnSuccessLogout(userId);
        }

        /**
         * {@inheritDoc}
         */
        //@Override
        public String CreateUser(String applicationName, String userName, String password, String email, String[] groups)
        {
            return _membershipRepository.CreateUser(applicationName, userName, password, email, groups);
        }

        /**
         * {@inheritDoc}
         */
        //@Override
        public void ModifyUserEmail(String userId, String email)
        {
            if (userId == null)
            {
                throw new UserNotFoundException("The userId should not be null");
            }
            _membershipRepository.ModifyUserEmail(userId, email);
        }

        /**
         * {@inheritDoc}
         */
        //@Override
        public bool ValidateUserPassword(String userId, String password)
        {
            if (userId == null || password == null)
            {
                throw new UserNotFoundException("The userId and password should not be null");
            }
            return _membershipRepository.ValidateUserPassword(userId, password);
        }

        /**
         * {@inheritDoc}
         */
        //@Override
        public void ModifyUserPassword(String userId, String password)
        {
            if (userId == null || password == null)
            {
                throw new UserNotFoundException("The userId and password should not be null");
            }
            _membershipRepository.ModifyUserPassword(userId, password);
        }

        /**
         * {@inheritDoc}
         */
        //@Override
        public IEnumerable<String> GetRolesForNavId(int navId)
        {
            return _membershipRepository.GetRolesForNavId(navId);
        }
    }
}